<?php
namespace App\Repositories;

use App\Models\Coach;
use App\Models\Evaluate;
use App\Models\Student;
use Carbon\Carbon;

class EvaluationRepository extends BaseRepository
{
    public $error = '';
    public function __construct(
        Evaluate $evaluate
    )
    {
        $this->model = $evaluate;
    }

    /**
     * 资源列表
     * @param  array $param 必须传入与模型查询相关的数据
     * @param  string|array $extra 可选额外传入的参数
     * @param  string $size 分页大小（存在默认值）
     * @return Illuminate\Support\Collection
     */
    public function index($param=[], $extra='', $size='10')
    {
        return $this->model->where('stunum',$extra->stunum)
                            ->orderBy('evaluatetime','desc')->get();
    }




    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }

    /**
     * 评价统计
     * @param $coach_id
     * @return array
     */
    public function statistics($coach_id)
    {
        $coach =  Coach::where('id',$coach_id)->first();
        $data = [
            'count'=>$coach->evaluation,
            'evaluationrate'=>$coach->evaluationrate,
            'satisfaction' => $this->model->where('evalobject',$coach->coachnum)->where('type',1)->whereIn('overall',[4,5])->count(),
            'general'=>$this->model->where('evalobject',$coach->coachnum)->where('type',1)->where('overall',3)->count(),
            'dissatisfaction'=>$this->model->where('evalobject',$coach->coachnum)->where('type',1)->whereIn('overall',[1,2])->count(),
        ];
        return $data;
    }

    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }


    /**
     * 驾校评论列表
     */
    public function schoolEvaluates($student_info, $type, $page = 1, $size = 10, $is_my = null)
    {
        $school_info = $student_info->school;
        $query =  $this->model->leftJoin('students', 'students.stunum', '=', 'evaluation.stunum')->select('evaluation.*','students.name','students.photo')->where('evalobject', $school_info->inscode)->where('type', 2);

        //非常满意
        if($type == 1){
            $query->where('overall', 5);
        //满意
        }elseif($type == 2){
            $query->whereBetween('overall', [2,4]);
        //一般
        }elseif($type == 3){
            $query->where('overall', 1);
        }


        if($is_my){
            $query->where('evaluation.stunum', $student_info->stunum);
        }

        return $query->orderBy('created_at', 'desc')->limit($size)->offset(($page - 1) * $size)->get();
    }

/**
* 教练评论列表
*/
    public function coachEvaluates($coach_info, $type, $page = 1, $size = 10)
    {
        $query =  $this->model->leftJoin('students', 'students.stunum', '=', 'evaluation.stunum')->select('evaluation.*','students.name','students.photo')->where('evalobject', $coach_info->coachnum)->where('type', 1);

        //非常满意
        if($type == 1){
            $query->where('overall', 5);
            //满意
        }elseif($type == 2){
            $query->whereBetween('overall', [2,4]);
            //一般
        }elseif($type == 3){
            $query->where('overall', 1);
        }

        return $query->limit($size)->offset(($page - 1) * $size)->get();
    }


    /**
     * 处理驾校评价
     */
    public  function evaluateSchool(Student $student, $param)
    {
        $this->model->school_id = $student->school_id;
        $this->model->stunum = $student->stunum;
        $this->model->evalobject = $student->school->inscode;
        $this->model->type = 2;
        $this->model->teachlevel = $param['content'];

//        0：不满意   1：满意   2：非常满意
        if($param['status'] == 1){
            $this->model->overall = 3;
        }elseif($param['status'] == 2){
            $this->model->overall = 5;
        }else{
            $this->model->overall = 1;
        }

        $this->model->anonymous = $param['anonymity'];

        return $this->model->save();
    }

}